/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.preview;

import java.io.File; 
import java.io.FileOutputStream;

import org.apache.commons.validator.GenericValidator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.mags.remas.editfile.ImageEditor;
import org.mags.remas.view.commons.ImageProvider;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class ImageView extends ImageEditor {
	public static final String ID = "org.mags.remas.preview.imageView";
	public void export() {
		Image image  = null;
		FileOutputStream stream = null;
		try {
			image = getImage();
			if(image!=null) {
				FileDialog dialog = new FileDialog(super.getSite().getShell(), SWT.SAVE);
				dialog.setText("Save As");
				dialog.setFilterExtensions(new String[] { "*.gif; *.jpg; *.png; *.ico; *.bmp" });
				dialog.setFilterNames(new String[] { "image" + " (gif, jpeg, png, ico, bmp)" });
				String selected = dialog.open();
				if(!GenericValidator.isBlankOrNull(selected)) {
					File file = new File(selected);
					if(file.exists()) {
						if(MessageDialog.openQuestion(super.getSite().getShell(), "The file exists", "Do you want rewrite the file?"))
							file.delete();
						else
							return;
					}
					ImageLoader loader = new ImageLoader();
					loader.data = new ImageData[]{image.getImageData()};
					stream = new FileOutputStream(selected);
					if(selected.toUpperCase().endsWith(".GIF"))
						loader.save(stream, SWT.IMAGE_GIF);
					else if(selected.toUpperCase().endsWith(".JPG"))
						loader.save(stream, SWT.IMAGE_JPEG);
					else if(selected.toUpperCase().endsWith(".PNG"))
						loader.save(stream, SWT.IMAGE_PNG);
					else if(selected.toUpperCase().endsWith(".ICO"))
						loader.save(stream, SWT.IMAGE_ICO);
					else if(selected.toUpperCase().endsWith(".BMP"))
						loader.save(stream, SWT.IMAGE_BMP);	
				}
			}
		} catch(Exception e) {
			if(e instanceof RuntimeException)
				throw (RuntimeException)e;
			else
				throw new RuntimeException(e);
		} finally {
			try {
				if(image!=null)
					image.dispose();
				if(stream!=null) {
					stream.close();
				}
			} catch(Exception e){/*IGNORE EXCEPTION*/}
		}
	}
	@Override
	protected void createToolBar(Composite parent) {
		ToolBar toolBar = new ToolBar(parent, SWT.FLAT);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.RIGHT;
		gridData.grabExcessHorizontalSpace = true;		
		toolBar.setLayoutData(gridData);

		ToolItem refreshButton = new ToolItem(toolBar, SWT.NONE);
		refreshButton.setText("refresh");
		refreshButton.setImage(ImageProvider.getCommonImage("refresh.png"));
		refreshButton.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			public void widgetSelected(SelectionEvent e) {
				try {
					refresh();
				} catch(Exception _e) {
					exceptionCatched(_e);
				}
			}
		});		

		ToolItem exportButton = new ToolItem(toolBar, SWT.NONE);
		exportButton.setText("export");
		exportButton.setImage(ImageProvider.getCommonImage("export.png"));
		exportButton.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			public void widgetSelected(SelectionEvent e) {
				try {
					export();
				} catch(Exception _e) {
					exceptionCatched(_e);
				}
			}
		});		

	}
}
